/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.Cursor;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.lua.Event;
import net.worlds.ui.UI;
import org.jbox2d.common.Vec2D;


public class CopyTool extends Tool{
    private GameObject lastClone;
    private boolean lastCloneNoCollide = false;
    private boolean lastCloneFixed = false;
    public CopyTool() throws WorldsException
    {
        super("CopyTool");
        addEvent("DragStarted", new Event(), "dragPart (GameObject)");
    }
    @Override
    public void selected()
    {
        if(!Main.noView)
        {
            UI.mouseMode = UI.MODE_ARROW;
            UI.setCursor(Cursor.getDefaultCursor());
            UI.setMouseImage("add");
        }
    }
    @Override
    public void mouseDown(int b, Vec2D pos)
    {
        if(b == 1)
        {
            try {
                super.mouseDown(b, pos);
                GameObject obj = ((World)world.getService("World")).getPartAt(pos);
                if(obj != null)
                {
                    GameObject clone = (GameObject)obj.clone(true);
                    clone.setParent(world.getService("World"));
                    lastClone = clone;
                    getEvent("DragStarted").fire(new Object[]{clone});
                    if(!lastClone.getBooleanProp("Collidable"))
                        lastCloneNoCollide = true;
                    else
                        lastClone.setProp("Collidable", false, true);
                    if(lastClone.getBooleanProp("Fixed"))
                        lastCloneFixed = true;
                    else
                        lastClone.setProp("Fixed", true, true);
                    ((Part)lastClone).getShape().m_isSensor = true;

                }
                if(b == 3 && UI.getSelected().size() == 1 && UI.getSelected().get(0) instanceof Part)
                {
                    try {
                        Part p = (Part) UI.getSelected().get(0);
                        if (p.getBooleanProp("Fixed") == true) {
                            p.setProp("Fixed", false, true);
                        } else {
                            p.setProp("Fixed", true, true);
                        }
                    } catch (WorldsException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            } catch (WorldsException ex) {
                throw new RuntimeException(ex);
            }
        }
        else if(b == 3)
        {
            try {
                Part p = ((World) world.getService("World")).getPartAt(pos);
                if(p == null)
                    return;
                if (p.getBooleanProp("Fixed") == true) {
                    p.setProp("Fixed", false, true);
                } else {
                    p.setProp("Fixed", true, true);
                }
            } catch (WorldsException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    @Override
    public void mouseUp(int b, Vec2D pos)
    {
        if(b == 1)
        {
            try {
                if (!lastCloneNoCollide && lastClone != null) {
                    lastClone.setProp("Collidable", true, true);
                }
                if(!lastCloneFixed && lastClone != null)
                    lastClone.setProp("Fixed", false, true);
            } catch (WorldsException ex) {
                ex.printStackTrace();
            }
        }
    }
    @Override
    public void deselected()
    {
        UI.clearMouseMode();
    }
}

/*
link(script.Parent.Selected, function() print("S") end)
link(script.Parent.Deselected, function() print("d") end)
link(script.Parent.MouseDown, function(b, p) print(b); print(p); end)
link(script.Parent.MouseUp, function(p) print(p) end)
 */